package com.bringway.p2p.controller;


import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.bringway.framework.controller.JsonController;
import com.bringway.framework.util.RequestUtils;
import com.bringway.global.domain.SysUserDetails;
import com.bringway.p2p.service.SecurityService;
import com.bringway.p2p.service.impl.SecurityServiceImpl;

import net.sf.json.JSONObject;

/*
 * @ClassName: SecurityController
 * @Description: 密保控制器
 * @author: qjy
 * @date: 2016年8月17日 下午13:17:54
 */
@Controller
@RequestMapping("/Security")
public class SecurityController extends JsonController {
	
	Logger logger = Logger.getLogger(SecurityController.class);
@Autowired
	private SecurityService securityService;

	
	
	/**
	 * 
	 * @param request 
	 * @param request
	 * @param response 查询密保问题数据
	 * @return
	 * @throws Exception
	 */

	@RequestMapping("/searchSecurity")
	public ModelAndView searchSecurity(HttpServletRequest request,HttpServletResponse response) throws Exception{
		
		JSONObject params = new JSONObject();
		
		List list = securityService.searchSecurity(params);
		JSONObject result = new JSONObject();
		result.put("data", list);
		serialize(request, response, result);
		return null;
	}
	


	/**
	 * @方法名称: searchSecurity
	 * @方法描述: 保存密保问题及答案
	 * @时间:2016年08月17日上午13:23:44
	 * @author:qjy
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/saveSecurity")
	public ModelAndView saveSecurity(HttpServletRequest request,HttpServletResponse response)throws Exception{
		String id = UUID.randomUUID().toString();
		String username = RequestUtils.getStringParameter(request, "username");
		String squsetion = RequestUtils.getStringParameter(request, "squsetion");
		String sanswer = RequestUtils.getStringParameter(request, "sanswer");
		JSONObject parms = new JSONObject();
		JSONObject result = new JSONObject();
		parms.put("id", id);
		parms.put("username", username);
		parms.put("squsetion", squsetion);
		parms.put("sanswer", sanswer);
		boolean bool = securityService.saveSecurity(parms);
		result.put("success",true);
		result.put("isAddS", bool);
		serialize(request, response, result);
		return null;
	}
	
	
	
	/**
	 * 
	 * @param request
	 * @param response :加载用户密保问题?根据用户名查询
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/searchSecuritylist")
	public ModelAndView searchSecuritylist(HttpServletRequest request,HttpServletResponse response) throws Exception{
		JSONObject result = new JSONObject();
		JSONObject params = new JSONObject();
		
		Integer start = RequestUtils.getIntParameter(request, "start");
		Integer limit = RequestUtils.getIntParameter(request, "limit");
		String username= RequestUtils.getStringParameter(request, "username");
		params.put("start", start);
		params.put("limit", limit);
		params.put("username", username);
		List<Map<String, Object>> list = securityService.searchSecuritylist(params);
		int count = securityService.getSecurity(params);
		result.put("data", list);	
		result.put("total", count);
		serialize(request,response,result);
		return null;
	}
	
	
	/**
	 * 
	 * @param request
	 * @param response :验证密保密码是否正确根据用户键入的值查询.
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/querySanswer")
	public ModelAndView querySanswer(HttpServletRequest request,HttpServletResponse response) throws Exception{
		String id = UUID.randomUUID().toString();
		String squestion_one= RequestUtils.getStringParameter(request, "squestion_one");
		String sanswer_one= RequestUtils.getStringParameter(request, "sanswer_one");
		String squestion_two= RequestUtils.getStringParameter(request, "squestion_two");
		String sanswer_two= RequestUtils.getStringParameter(request, "sanswer_two");
		String squestion_three= RequestUtils.getStringParameter(request, "squestion_three");
		String sanswer_three= RequestUtils.getStringParameter(request, "sanswer_three");
		JSONObject params = new JSONObject();
		params.put("id", id);
		params.put("squestion_one", squestion_one);
		params.put("sanswer_one", sanswer_one);
		params.put("squestion_two", squestion_two);
		params.put("sanswer_two", sanswer_two);
		params.put("squestion_three", squestion_three);
		params.put("sanswer_three", sanswer_three);
		List list = securityService.querySanswer(params);
		JSONObject result = new JSONObject();
		result.put("data", list);
		serialize(request, response, result);
		return null;
	}
}
